package com.mydemo.springlearning.functions.mail_send.controller;

import com.mydemo.springlearning.functions.mail_send.entity.Mail;
import com.mydemo.springlearning.functions.mail_send.service.MailTestService;
import com.mydemo.springlearning.common.entity.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hd
 * @date 2022-07-19 17:24
 * @description 邮件测试
 */
@RestController
@RequestMapping("mail")
public class MailController {
    @Autowired
    private MailTestService mailTestService;

    @PostMapping("send")
    public ServerResponse sendMail(@Validated Mail mail, Errors errors) {
        if (errors.hasErrors()) {
            String msg = errors.getFieldError().getDefaultMessage();
            return ServerResponse.error(msg);
        }

        return mailTestService.send(mail);
    }
}
